home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_11 / phillip2 / ht.c < prev    next >
C/C++ Source or Header  |  1993-06-18  |  16KB  |  603 lines

  1.  
  2.  
  3.    /**************************************************
  4.    *
  5.    *   file d:\cips\ht.c
  6.    *
  7.    *   Functions: This file contains
  8.    *       display_using_halftoning
  9.    *       half_tone
  10.    *       show_half_tone
  11.    *       get_threshold_value
  12.    *
  13.    *   Purpose: This program displays an image using 
  14.    *      a halftoning process.  The algorithm was 
  15.    *      taken from "Personal computer based image 
  16.    *      processing with halftoning," John A Saghri, 
  17.    *      Hsieh S. Hou, Andrew Tescher, Optical 
  18.    *      Engineering, March 1986, Vol.25, No. 3, 
  19.    *      pp 499-503. The display_using_halftoning
  20.    *      determines display size and reads the image.
  21.    *      The half_tone function implements the 
  22.    *      algorithm shown on page 502 of the article.
  23.    *
  24.    *      The function print_halftone_array prints 
  25.    *      a half toned image array to a regular line 
  26.    *      printer.
  27.    *
  28.    *
  29.    *   External Calls:
  30.    *      rtiff.c - read_tiff_image
  31.    *      numcvrt.c - get_integer
  32.    *
  33.    *   Modifications:
  34.    *      30 September 86 - created
  35.    *      18 August 1990 - modified for use in the
  36.    *          C Image Processing System.
  37.    *
  38.    *
  39.    **************************************************/
  40.  
  41.  
  42. #include "cips.h"
  43.  
  44. #define  FORMFEED  '\014'
  45.  
  46. float eg[ROWS][COLS], ep[ROWS][COLS];
  47.  
  48. display_using_halftoning(in_image, file_name,
  49.          il, ie, ll, le, threshold, invert,
  50.          image_colors, image_header, monitor_type,
  51.          print, show_hist, color_transform)
  52.  
  53.    char  color_transform[], file_name[], 
  54.          monitor_type[];
  55.    int   image_colors, invert,
  56.          il, ie, ll, le, threshold,
  57.          print, show_hist;
  58.    short in_image[ROWS][COLS];
  59.    struct tiff_header_struct *image_header;
  60.  
  61. {
  62.    char response[80];
  63.  
  64.    int  a,
  65.         b,
  66.         channel,
  67.         color,
  68.         count,
  69.         data_color,
  70.         display_mode,
  71.         horizontal,
  72.         i,
  73.         j,
  74.         k,
  75.         l,
  76.         line_color,
  77.         max_horizontal,
  78.         max_vertical,
  79.         not_finished,
  80.         one,
  81.         vertical,
  82.         x,
  83.         x_offset,
  84.         y,
  85.         y_offset,
  86.         zero;
  87.  
  88.    float area, new_grays;
  89.  
  90.    unsigned long histogram[256], new_hist[256];
  91.  
  92.  
  93.        if(  (show_hist == 1)  &&
  94.             (color_transform[0] != 'H'))
  95.           zero_histogram(histogram);
  96.  
  97.          /*******************************************
  98.          *
  99.          *   Use the monitor type to set the vertical
  100.          *   horizontal and display_mode parameters.
  101.          *   Also set the values for one and zero.
  102.          *   one and zero will vary depending on the
  103.          *   monitor type.
  104.          *
  105.          ********************************************/
  106.  
  107.  
  108.       if(  (monitor_type[0] == 'M')  ||
  109.            (monitor_type[0] == 'm')){
  110.          vertical     = 3;
  111.          horizontal   = 2;
  112.          display_mode = HRESBW;
  113.          one          = 1;
  114.          zero         = 0;
  115.       }
  116.  
  117.       if(  (monitor_type[0] == 'C')  ||
  118.            (monitor_type[0] == 'c')){
  119.          vertical     = 3;
  120.          horizontal   = 2;
  121.          display_mode = MRES4COLOR;
  122.          one          = 3;
  123.          zero         = 1;
  124.       }
  125.  
  126.       if(  (monitor_type[0] == 'V')  ||
  127.            (monitor_type[0] == 'v')){
  128.          vertical     = 6;
  129.          horizontal   = 4;
  130.          display_mode = VRES16COLOR;
  131.          one          = 5;
  132.          zero         = 1;
  133.       }
  134.  
  135.       if(  (monitor_type[0] == 'E')  ||
  136.            (monitor_type[0] == 'e')){
  137.          vertical     = 6;
  138.          horizontal   = 3;
  139.          display_mode = ERESCOLOR;
  140.          one          = 5;
  141.          zero         = 1;
  142.       }
  143.  
  144.       max_horizontal = (image_header->image_length+50)
  145.                          /COLS;
  146.       max_vertical   = (image_header->image_width+50)
  147.                          /ROWS;
  148.  
  149.       if(horizontal > max_horizontal) 
  150.             horizontal = max_horizontal;
  151.       if(vertical > max_vertical) 
  152.             vertical = max_vertical;
  153.  
  154.       if(print == 1){
  155.          vertical   = 1;
  156.          horizontal = 1;
  157.       }
  158.  
  159.  
  160.  
  161.         /****************************************
  162.         *
  163.         *   If color transform wants histogram
  164.         *   equalization, then read in the
  165.         *   image arrays and calculate the
  166.         *   histogram.   Zero both the histogram
  167.         *   and the new_hist.  You will need the
  168.         *   new_hist if you want to display the
  169.         *   equalized hist.
  170.         *
  171.         *****************************************/
  172.  
  173.       if(color_transform[0] == 'H'){
  174.          count = 1;
  175.          zero_histogram(histogram);
  176.          zero_histogram(new_hist);
  177.          for(a=0; a<vertical; a++){
  178.             for(b=0; b<horizontal; b++){
  179.  
  180.                x = a*COLS;
  181.                y = b*ROWS;
  182.  
  183.                printf("\nHT> Calculating histogram");
  184.                printf(" %d of %d",
  185.                       count,vertical*horizontal);
  186.                count++;
  187.  
  188.                read_tiff_image(file_name, in_image, 
  189.                             il+y, ie+x, ll+y, le+x);
  190.                calculate_histogram(in_image, 
  191.                                    histogram);
  192.  
  193.             }  /* ends loop over b */
  194.          }  /* ends loop over a */
  195.       }  /* ends if display_mode == H */
  196.  
  197.  
  198.  
  199.  
  200.            /* set graphics mode */
  201.       if(print == 0)
  202.          my_setvideomode(display_mode); /* MSC 6.0 */
  203.       else{
  204.          printf("\n\nHT> Calculating for printing ");
  205.          printf("\nHT> Counting from 0 to 99\n");
  206.       }
  207.  
  208.         /********************************************
  209.         *
  210.         *   Loop over horizontal and vertical. Read
  211.         *   the image array and display it after
  212.         *   calculating the half tone values.
  213.         *
  214.         *
  215.         *   If you want to show the histogram AND
  216.         *   do not want to do hist equalization
  217.         *   then calculate the hist from the
  218.         *   original image array.
  219.         *
  220.         *   If you want to do hist equalization
  221.         *   then calculate the new_hist AFTER
  222.         *   the image has been equalized.
  223.         *
  224.         *   We will equalize the histogram down
  225.         *   to half the original shades of gray
  226.         *   and will cut the threshold in half.
  227.         *
  228.         *****************************************/
  229.  
  230.  
  231.  
  232.       for(i=0; i<horizontal; i++){
  233.          for(j=0; j<vertical; j++){
  234.             read_tiff_image(file_name, in_image, 
  235.                            il+i*ROWS, ie+j*COLS, 
  236.                            ll+i*ROWS, le+j*COLS);
  237.  
  238.             if(   (show_hist == 1)  &&
  239.                   (color_transform[0] != 'H'))
  240.                calculate_histogram(in_image, histogram);
  241.  
  242.             if(color_transform[0] == 'H'){
  243.  
  244.                area = ((long)(vertical))*
  245.                       ((long)(horizontal));
  246.                area = area*10000.0;
  247.                new_grays = image_colors/2;
  248.  
  249.                perform_histogram_equalization(in_image,
  250.                         histogram, new_grays, area);
  251.  
  252.                calculate_histogram(in_image, new_hist);
  253.  
  254.             }  /* ends if color_transform == S */
  255.  
  256.             if(color_transform[0] == 'H')
  257.                half_tone(in_image, threshold/2, 
  258.                       eg, ep, i, j,
  259.                       one, zero, invert, print);
  260.  
  261.             else
  262.                half_tone(in_image, threshold, 
  263.                       eg, ep, i, j,
  264.                       one, zero, invert, print);
  265.  
  266.          }  /* ends loop over j */
  267.       }  /*  ends loop over i */
  268.  
  269.  
  270.  
  271.          /***************************
  272.          *
  273.          *   if show_hist == 1 then
  274.          *   display the histogram
  275.          *   in the lower right hand
  276.          *   corner of screen
  277.          *
  278.          ****************************/
  279.  
  280.       if(  (show_hist == 1)   &&
  281.            (print == 0)){
  282.  
  283.          if(monitor_type[0] == 'V'){
  284.             y_offset   = 470;
  285.             x_offset   = 380;
  286.             line_color = 3;
  287.             data_color = 8;
  288.          }
  289.  
  290.          if(monitor_type[0] == 'E'){
  291.             y_o